package com.lb.activity.config.shiro;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.lb.activity.mapper.UserMapper;
import com.lb.activity.po.User;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created with IntelliJ IDEA.
 * Description: 验证密码
 * User: liubin
 * Date: 2017-11-02
 * Time: 上午 10:03
 * @author 001030
 */
public class CredentialsMatcher extends SimpleCredentialsMatcher{

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken utoken = (UsernamePasswordToken) token;
        //获取用户输入的密码（可以采用加盐的方式）
        User userLogin = userMapper.getUserByUserName(utoken.getUsername());
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, userLogin.getSalt());
        //将前端传递的密码  与盐加密后进行比对
        String inPassWord = aes.encryptHex(new String(utoken.getPassword()));
        //获取数据库的密码
        String dbPassWord = (String) info.getCredentials();

        //进行比对  注意toString
        return this.equals(inPassWord,dbPassWord);
    }



}